package com.example.volunteeroa.controller;

import com.example.volunteeroa.JpaUtils.DeleteUtils;
import com.example.volunteeroa.entity.*;
import com.example.volunteeroa.repository.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@CrossOrigin//跨域
@RestController
@RequestMapping("api/venueAllocation")

public class VenueAllocationController {
    @Resource
    VenueRepository VERepository;
    @Resource
    VenueAllocationRepository VARepository;

    @PostMapping("/findOrgByVenueID")
    public RestBean findOrgAccordingVenueID(@RequestBody Venue venue){
        Optional<Venue> byId = VERepository.findById(venue.getVenueID());
        if (byId.isPresent()){
            Venue venue1 = byId.get();
            List<Organization> organizations = venue1.getOrganizations();
            return new RestBean(200,"查询到了场馆下的组织",organizations);
        }else {
            return new RestBean(500,"组织不存在",null);
        }
    }//通过venueID查找venue下被分配到到所有organization

    @DeleteMapping()
    public RestBean deleteVenueByID(@RequestBody Venue venue) {
        DeleteUtils.deleteVenueAllocByVenueID(VERepository,VARepository,venue);
        return new RestBean(200, "删除场馆下的分配成功", null);
    }//删除场馆下的分配，但是不删除场馆

    @PostMapping("/add/{venueID}")
    public RestBean add(@PathVariable Integer venueID,@RequestBody List<VenueAllocation> venueAllocation){
        for(int i=0;i<venueAllocation.size();i++){
            VenueAllocation venueAllocation1=venueAllocation.get(i);
            Optional<Venue> byID=VERepository.findById(venueID);
            venueAllocation1.setVenue(byID.get());
            VARepository.save(venueAllocation1);
        }
        return new RestBean(200,"添加成功",null);
    }


}
